from datetime import datetime
from typing import List

from pydantic import Field, field_validator

from forward.extend.pydantic.base import BaseSchema


class ActivityCodeNoSchema(BaseSchema):
    activity_id: int | None = None
    code_id_start: int | None = None
    code_id_end: int | None = None
    prefix: str | None = None
    total_num: int | None = None


class ActivityCodeNoOutSchema(ActivityCodeNoSchema):
    id: int


class ActivityPrizeSchema(BaseSchema):
    # id: int | None = None
    activity_id: int
    prize_id: int
    prize_level_id: str | None = None
    prize_index: int = Field(description="奖品索引==一等奖 这样的序号，谢谢惠顾是0")
    num: int
    rellot_num: int | None = None
    probability: float | None = None


class ActivityPrizeOutSchema(ActivityPrizeSchema):
    id: int


class ActivitySchema(BaseSchema):
    type: int | None = None
    title: str | None = None
    merchant_id: int | None = None
    num: int
    start_time: datetime
    end_time: datetime
    is_bind_client_phone: int | None = None
    is_bind_client_name: int | None = None
    is_bind_client_address: int | None = None
    is_custom_prize_img: int | None = None
    day_limit_num: int | None = None
    client_limit_num: int
    status: int | None = None
    lock: int | None = None
    remark: str | None = None
    rules_desc: List[str] | None = None
    draw_id: int
    prefix: str | None = None
    start_code_id: int | None = None
    end_code_id: int | None = None
    kefu_qr_img: str | None = None
    kefu_phone: str | None = None
    qrcode_img_path: str | None = None

    @field_validator("rules_desc", mode="before")
    def srt_to_list(cls, v):
        if type(v) == str:
            return v.split(",")
        return v


class ActivityOutSchema(ActivitySchema):
    id: int
    merchant: str | None = None
    activity_prizes: list[ActivityPrizeOutSchema] | None = None
